﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wUpdate.xaml
    /// </summary>
    public partial class wUpdate : Window
    {
        public wUpdate()
        {
            InitializeComponent();
        }

        public void Update()
        {
            bool bFail = false;
            string err="";
            Process myProcess = new Process();
            if (Properties.Settings.Default.update54 == false)
            {
                try
                {
                    myProcess.StartInfo.UseShellExecute = false;
                    // You can start any process, HelloWorld is a do-nothing example.

                    myProcess.StartInfo.FileName = @"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SqlCmd.exe";
                    myProcess.StartInfo.Arguments = @"-S .\ROUTELIST -i dbRouteList54.sql";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();
                    // This code assumes the process you are starting will terminate itself. 

                    // Given that is is started without a window so you cannot terminate it 

                    // on the desktop, it must terminate itself or you can do it programmatically

                    // from this application using the Kill method.
                    Properties.Settings.Default.update54 = true;
                    Properties.Settings.Default.Save();

                }
                catch (Exception ex)
                {
                    bFail = true;
                    err = (ex.ToString());
                }

                if (bFail)
                {
                    try
                    {
                        myProcess = new Process();
                        myProcess.StartInfo.UseShellExecute = false;
                        // You can start any process, HelloWorld is a do-nothing example.

                        myProcess.StartInfo.FileName = @"C:\Program Files\Microsoft SQL Server\MSSQL10.ROUTELIST\MSSQL\Binn\SqlCmd.exe";
                        myProcess.StartInfo.Arguments = @"-S .\ROUTELIST -i dbRouteList54.sql";
                        myProcess.StartInfo.CreateNoWindow = true;
                        myProcess.Start();
                        // This code assumes the process you are starting will terminate itself. 

                        // Given that is is started without a window so you cannot terminate it 

                        // on the desktop, it must terminate itself or you can do it programmatically

                        // from this application using the Kill method.
                        Properties.Settings.Default.update54 = true;
                        Properties.Settings.Default.Save();

                    }
                    catch (Exception ex)
                    {
                        bFail = true;
                        err = (ex.ToString());
                    }
                }

                if (bFail)
                    MessageBox.Show(err);
            }
            bFail = false;
            //Properties.Settings.Default.update58 = false;
            //Properties.Settings.Default.Save();
            if (Properties.Settings.Default.update58 == false)
            {
                try
                {
                    //MessageBox.Show("Обновляется");
                    myProcess = new Process();

                    myProcess.StartInfo.FileName = @"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SqlCmd.exe";
                    myProcess.StartInfo.Arguments = @"-S .\ROUTELIST -i dbRouteList58.sql";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();
                    routelistEntities db = new routelistEntities();
                    var rl = from o in db.RouteList select o;
                    foreach (RouteList item in rl)
                    {
                        try
                        {
                            DateTime td = DateTime.Now;
                            DateTime.TryParse(item.datedoc, out td);
                            item.dt_datedoc = td;
                        }
                        catch (Exception )
                        {
                        }
                    }
                    db.SaveChanges();
                    Properties.Settings.Default.update58 = true;
                    Properties.Settings.Default.Save();
                    MessageBox.Show("Обновлена схема данных");
                }
                catch (Exception)
                {
                    bFail = true;
                }
            }
            if (bFail)
                MessageBox.Show(err);

            //bFail = false;
            //try
            //{
            //    if (Properties.Settings.Default.update59 == false)
            //    {
            //        routelistEntities routelistEntities = new RL.routelistEntities();
            //        var rl = from o in routelistEntities.RouteList select o;
            //        foreach (RouteList r in rl)
            //        {
            //            if (r.dt_datedoc.HasValue)
            //                r.dt_datedoc = r.dt_datedoc.Value.Date;
            //        }
            //        routelistEntities.SaveChanges();
            //        Properties.Settings.Default.update59 = true;
            //        Properties.Settings.Default.Save();
            //    }

            //}
            //catch (Exception ex)
            //{
            //    err = ex.ToString();
            //    bFail = true;
            //}
            //if (bFail)
            //    MessageBox.Show(err);


            bFail = false;
            //Properties.Settings.Default.update58 = false;
            //Properties.Settings.Default.Save();
            if (Properties.Settings.Default.update74 == false)
            {
                try
                {
                    //MessageBox.Show("Обновляется");
                    myProcess = new Process();

                    myProcess.StartInfo.FileName = @"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SqlCmd.exe";
                    myProcess.StartInfo.Arguments = @"-S .\ROUTELIST -i dbRouteList74.sql";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();
                    routelistEntities db = new routelistEntities();
                    var rl = from o in db.RouteList select o;
                    foreach (RouteList item in rl)
                    {
                        try
                        {
                            DateTime td = DateTime.Now;
                            DateTime.TryParse(item.datedoc, out td);
                            item.dt_datedoc = td;
                        }
                        catch (Exception)
                        {
                        }
                    }
                    db.SaveChanges();
                    Properties.Settings.Default.update74 = true;
                    Properties.Settings.Default.Save();
                    MessageBox.Show("Обновлена схема данных");
                }
                catch (Exception)
                {
                    bFail = true;
                }
            }
            if (bFail)
                MessageBox.Show(err);

            if (Properties.Settings.Default.InsuranceReminder == true)
            {
                wInsuaranceReminder wnd = new wInsuaranceReminder();
                wnd.ShowDialog();
            }

            try
            {
                DialogResult = true;
            }
            catch (Exception)
            {
            }

            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }
        
    }
}
